﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;

namespace BT_web__nhom06
{
    public partial class Sua_ThongTinDV : System.Web.UI.Page
    {
        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string masinhvien = Request.QueryString["id"];
                if (masinhvien != null)
                {
                    DataTable tbl = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu("SELECT * FROM doanvien where masinhvien=@masinhvien", "@masinhvien", masinhvien);
                    if (tbl.Rows.Count > 0)
                    {
                        DataRow dr = tbl.Rows[0];
                        txt_masinhvien.Text = masinhvien;
                        txtho.Text = (string)dr["Ho"];
                        txtten.Text = (string)dr["Ten"];
                        if (dr.IsNull("NgaySinh") == false)
                            txtNgaySinh.Text = ((DateTime)dr["ngaysinh"]).ToString("dd/MM/yyyy");
                        else
                            txtNgaySinh.Text = "";
                        bool gioiTinh = (bool)dr["GioiTinh"];
                        if (gioiTinh)
                            rdbgioitinh.SelectedIndex = 0;
                        else
                            rdbgioitinh.SelectedIndex = 1;
                        if (dr.IsNull("Ngayvaodoan") == false)
                            txtngayvaodoan.Text = ((DateTime)dr["ngayvaodoan"]).ToString("dd/MM/yyyy");
                        else
                            txtngayvaodoan.Text = "";
                        txtdiachithuongtru.Text = dr["diachithuongtru"].ToString();
                        txtdiachihientai.Text = dr["diachinoiohientai"].ToString();
                        txtdienthoailienhe.Text = dr["dienthoailienhe"].ToString();
                        txtemail.Text = dr["email"].ToString();
                        dlstchidoan.SelectedValue = dr["machidoan"].ToString();
                        txtnoichuyenden.Text = dr["noichuyenden"].ToString();
                        if (dr.IsNull("Ngaychuyenden") == false)
                            txtngaychuyenden.Text = ((DateTime)dr["ngaychuyenden"]).ToString("dd/MM/yyyy");
                        else
                            txtngaychuyenden.Text = "";
                        txtnoichuyendi.Text = dr["noichuyendi"].ToString();
                        if (dr.IsNull("Ngaychuyendi") == false)
                            txtngaychuyendi.Text = ((DateTime)dr["ngaychuyendi"]).ToString("dd/MM/yyyy");
                        else
                            txtngaychuyendi.Text = "";
                        dlsttinhtrang.SelectedValue = dr["tinhtrang"].ToString();
                    }
                }
                else
                    Response.Redirect("QL_ThongTinDV.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string masinhvien = Request.QueryString["id"];
            int ketqua = 0;
            if(masinhvien!=null)
            {
                string ho = txtho.Text;
                string ten = txtten.Text;
                string hinhanh = "";
                DateTime ngaySinh = DateTime.Parse(txtNgaySinh.Text, new CultureInfo("fr-FR"));
                //hinh anh
                if (fuhinhanh.FileName != "")
                {
                    if (fuhinhanh.HasFile)
                    {
                        if (CheckFileType(fuhinhanh.FileName))
                        {
                            string fileName = Path.ChangeExtension(masinhvien, Path.GetExtension(fuhinhanh.FileName));
                            String filePath = Path.Combine("~/images_DV/", fileName);
                            string physicalPath = Server.MapPath(filePath);
                            fuhinhanh.SaveAs(physicalPath);
                            hinhanh = filePath;
                        }
                    }
                }
                else
                {
                    DataTable tbl = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu("SELECT * FROM doanvien where masinhvien=@masinhvien", "@masinhvien", masinhvien);
                    DataRow dr = tbl.Rows[0];
                    hinhanh = dr["hinhanh"].ToString();
                }
                    
                
                //hinh anh
                //gioitinh
                bool phai = true;
                if (rdbgioitinh.SelectedIndex == 1)
                    phai = false;
                DateTime ngayvaodoan = DateTime.Parse(txtngayvaodoan.Text, new CultureInfo("fr-FR"));
                string diachithuongtru = txtdiachithuongtru.Text;
                string diachihientai = txtdiachihientai.Text;
                string dienthoailienhe = txtdienthoailienhe.Text;
                string email = txtemail.Text;
                string chidoan = dlstchidoan.SelectedValue.ToString();
                string noichuyenden = txtnoichuyenden.Text;
                DateTime ngaychuyenden;
                DateTime ngaychuyendi;
                if (txtngaychuyenden.Text != "")
                {
                    ngaychuyenden = DateTime.Parse(txtngaychuyenden.Text, new CultureInfo("fr-FR"));
                }
                else
                    ngaychuyenden = new DateTime(1900, 1, 1);

                if (txtngaychuyendi.Text != "")
                {
                    ngaychuyendi = DateTime.Parse(txtngaychuyendi.Text, new CultureInfo("fr-FR"));
                }
                else
                    ngaychuyendi = new DateTime(1900, 1, 1);
                string noichuyendi = txtnoichuyendi.Text.ToString();
                string tinhtrang = dlsttinhtrang.SelectedValue.ToString();
                string lenhsql = "update doanvien set [ho] = @ho, [ten]=@ten,[hinhanh]=@hinhanh, [ngaysinh]=@ngaysinh,[gioitinh]=@phai, [ngayvaodoan]=@ngayvaodoan, [diachithuongtru]=@diachithuongtru,[diachinoiohientai]=@diachihientai,[dienthoailienhe]=@dienthoailienhe,[email]=@email,[machidoan]=@chidoan,[noichuyenden]=@noichuyenden,[ngaychuyenden]=@ngaychuyenden,[noichuyendi]=@noichuyendi,[ngaychuyendi]=@ngaychuyendi,[tinhtrang]=@tinhtrang where [masinhvien] = @masinhvien";
                ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(lenhsql,new string[]{"@ho","@ten","@hinhanh","@ngaysinh","@phai","@ngayvaodoan","@diachithuongtru","@diachihientai","@dienthoailienhe","@email","@chidoan","@noichuyenden","@ngaychuyenden","@noichuyendi","@ngaychuyendi","@tinhtrang","@masinhvien"},new object[]{ho,ten,hinhanh,ngaySinh,phai,ngayvaodoan,diachithuongtru,diachihientai,dienthoailienhe,email,chidoan,noichuyenden,ngaychuyenden,noichuyendi,ngaychuyendi,tinhtrang,masinhvien});
                if (ketqua==1)
                    Label1.Text = "cap nhat thanh cong!";
                else
                    Label1.Text = "cap nhat khong thanh cong!";
            }
                
        }

        protected void dlstchidoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void RadioButton1_CheckedChanged1(object sender, EventArgs e)
        {

        }

        protected void txtNgaySinh_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("QL_ThongTinDV.aspx");
        }
    }
}